#include "Fenetre.h"
#include <stdlib.h>
#include <time.h> 

Fenetre::Fenetre() {
	setFocusPolicy(Qt::StrongFocus);
	srand (time(NULL));

	app =  new Application;
	timer = new QTimer(this);

	loadImages();
	loadMusique();
	setConsole();
	setTemperature();
	setBoutons();
	setEtats();
	setChronometre();
	setLayoutPrincipal();

	timer->start(1000);
}

void Fenetre::keyPressEvent(QKeyEvent *e) {
	if ( e->key() == Qt::Key_L ) {
		lumiere();
	} else if ( e->key() == Qt::Key_M ) {
		musique();
	} else if ( e->key() == Qt::Key_A ) {
		affichage();
	} else if ( e->key() == Qt::Key_O ) {
		okFPGA();
	} else if ( e->key() == Qt::Key_P ) {
		problemeFPGA("Erreur de jambon");
	}
}

/*
	SLOTS
*/
void Fenetre::lumiere() {
	app->toggleLumiere();
	if ( app->getEtatLumiere() == Eteinte ) {
		txtZoneTexte->setText(txtZoneTexte->toPlainText() + "\nLumiere eteinte !");
		lblLumiere->setStyleSheet("border: 10px solid black");

		if ( !imgLumiereEteinte->isNull() ) {
			lblLumiere->setPixmap(QPixmap::fromImage(*imgLumiereEteinte));
		}
	} else if ( app->getEtatLumiere() == Allumee ) {
		txtZoneTexte->setText(txtZoneTexte->toPlainText() + "\nLumiere allumee !");
		lblLumiere->setStyleSheet("border: 10px solid yellow");

		if ( !imgLumiereAllumee->isNull() ) {
			lblLumiere->setPixmap(QPixmap::fromImage(*imgLumiereAllumee));
		}
	}

	QTextCursor curseur = txtZoneTexte->textCursor();
	curseur.movePosition(QTextCursor::End);
	txtZoneTexte->setTextCursor(curseur);
}

void Fenetre::musique() {
	app->toggleMusique();
	if ( app->getEtatMusique() == Lecture ) {
		txtZoneTexte->setText(txtZoneTexte->toPlainText() + "\nMusique en lecture !");
		lblMusique->setStyleSheet("border: 10px solid green");

		if ( !imgMusiqueLecture->isNull() ) {
			lblMusique->setPixmap(QPixmap::fromImage(*imgMusiqueLecture));
		}

		sndMusique->play();
	} else if ( app->getEtatMusique() == Pause ) {
		txtZoneTexte->setText(txtZoneTexte->toPlainText() + "\nMusique en arret !");
		lblMusique->setStyleSheet("border: 10px solid red");

		if ( !imgMusiqueStop->isNull() ) {
			lblMusique->setPixmap(QPixmap::fromImage(*imgMusiqueStop));
		}

		sndMusique->stop();
	}

	QTextCursor curseur = txtZoneTexte->textCursor();
	curseur.movePosition(QTextCursor::End);
	txtZoneTexte->setTextCursor(curseur);
}

void Fenetre::affichage() {
	app->toggleAffichage();
	if ( app->getEtatAffichage() == Camera ) {
		txtZoneTexte->setText(txtZoneTexte->toPlainText() + "\nCamera !");

		txtTempTexte->hide();
		lblAffichage->show();

		timerTemperature->stop();
		txtTempTexte->setText("");
	} else if ( app->getEtatAffichage() == Temperatures ) {
		txtZoneTexte->setText(txtZoneTexte->toPlainText() + "\nTemperatures !");

		lblAffichage->hide();
		txtTempTexte->show();

		timerTemperature->start(1500);
	}

	QTextCursor curseur = txtZoneTexte->textCursor();
	curseur.movePosition(QTextCursor::End);
	txtZoneTexte->setTextCursor(curseur);
}

void Fenetre::temperatureGestion() {
	int temps = 0;
	temps = getTemperature();

	txtTempTexte->setText(txtTempTexte->toPlainText() + "Temperature : " + QString::number(temps) + "C\n");
	QTextCursor curseur = txtTempTexte->textCursor();
	curseur.movePosition(QTextCursor::End);
	txtTempTexte->setTextCursor(curseur);

	temps = getTemperature();
}

void Fenetre::chronometre(){
	//Compteur pour passer de ms en hh mm ss
	int secs = temps.elapsed() / 1000;
	int mins = (secs / 60) % 60;
	int hours = (secs / 3600);
	secs = secs % 60;

	//Update
	chrono ->setText(QString::number(hours) + "H " + QString::number(mins) + "M " + QString::number(secs) + "S");
}

void Fenetre::okFPGA() {
	lblFPGA->setText("OK");
	lblFPGA->setAlignment(Qt::AlignCenter);
	lblFPGA->setStyleSheet("border: 10px solid green");

	txtZoneTexte->setText(txtZoneTexte->toPlainText() + "\n\nConnection au FPGA retablie !\n");
	QTextCursor curseur = txtZoneTexte->textCursor();
	curseur.movePosition(QTextCursor::End);
	txtZoneTexte->setTextCursor(curseur);
}

void Fenetre::problemeFPGA(string msg) {
	lblFPGA->setText(QString(msg.c_str()));
	lblFPGA->setAlignment(Qt::AlignCenter);
	lblFPGA->setStyleSheet("border: 10px solid red");

	txtZoneTexte->setText(txtZoneTexte->toPlainText() + "\n\nProbleme detecte sur le FPGA :\n");
	txtZoneTexte->setText(txtZoneTexte->toPlainText() + QString(msg.c_str()) + "\n");
	QTextCursor curseur = txtZoneTexte->textCursor();
	curseur.movePosition(QTextCursor::End);
	txtZoneTexte->setTextCursor(curseur);
}

/*
	Methodes privees
*/
void Fenetre::loadImages() {
	imgLumiereAllumee = new QImage(":/Images/Lumiere_Allumee.png");
	imgLumiereEteinte = new QImage(":/Images/Lumiere_Eteinte.png");

	imgMusiqueLecture = new QImage(":/Images/Musique_Lecture.png");
	imgMusiqueStop = new QImage(":/Images/Musique_Stop.png");

	imgAffichageCamera = new QImage(":/Images/Camera.png");
	imgAffichageTemperature = new QImage(":/Images/Temperature.png");
}

void Fenetre::loadMusique() {
	sndMusique = new QSound("./Sons/mitis.wav");
}

void Fenetre::setConsole() {
	txtZoneTexte = new QTextEdit;
	txtZoneTexte->setTextInteractionFlags(Qt::NoTextInteraction);
	txtZoneTexte -> setText("/*\n *   MultiCasque Pro\n *   Developpe par l'equipe P12\n *   Version 0.36\n*/\n");
}

void Fenetre::setTemperature() {
	timerTemperature = new QTimer(this);
	txtTempTexte = new QTextEdit;
	txtTempTexte->setTextInteractionFlags(Qt::NoTextInteraction);

	connect(timerTemperature, SIGNAL(timeout()), this, SLOT(temperatureGestion()));
}

void Fenetre::setBoutons() {
	boxBoutons = new QGroupBox("Commandes");
	QHBoxLayout *layout = new QHBoxLayout;

	btnAffichage = new QPushButton("Affichage");
	btnLumiere = new QPushButton("Lumiere");
	btnMusique = new QPushButton("Musique");

	connect(btnAffichage, SIGNAL(clicked()), this, SLOT(affichage()));
	connect(btnLumiere, SIGNAL(clicked()), this, SLOT(lumiere()));
	connect(btnMusique, SIGNAL(clicked()), this, SLOT(musique()));

	layout -> addWidget(btnAffichage);
	layout -> addWidget(btnLumiere);
	layout -> addWidget(btnMusique);

	boxBoutons -> setLayout(layout);
}

void Fenetre::setEtats() {
	boxAffichage = new QGroupBox("Affichage");
	boxEtats = new QGroupBox("Etats");
	boxFPGA = new QGroupBox("FPGA");
	lytAffichage = new QHBoxLayout;
	QHBoxLayout *layout = new QHBoxLayout;
	QHBoxLayout *layout3 = new QHBoxLayout;

	lblLumiere = new QLabel;
	lblLumiere->setStyleSheet("border: 10px solid black");
	if ( !imgLumiereEteinte->isNull() ) {
		lblLumiere->setPixmap(QPixmap::fromImage(*imgLumiereEteinte));
	}
	lblLumiere->setScaledContents(true);

	lblMusique = new QLabel;
	lblMusique->setStyleSheet("border: 10px solid red");
	if ( !imgMusiqueStop->isNull() ) {
		lblMusique->setPixmap(QPixmap::fromImage(*imgMusiqueStop));
	}
	lblMusique->setScaledContents(true);

	lblAffichage = new QLabel;
	lblAffichage->setStyleSheet("border: 10px solid black");
	if ( !imgAffichageCamera->isNull() ) {
		lblAffichage->setPixmap(QPixmap::fromImage(*imgAffichageCamera));
	}
	lblAffichage->setScaledContents(true);

	lblFPGA = new QLabel("OK");
	lblFPGA->setAlignment(Qt::AlignCenter);
	lblFPGA->setStyleSheet("border: 10px solid green");
	
	lytAffichage->addWidget(lblAffichage);
	lytAffichage->addWidget(txtTempTexte);
	txtTempTexte->hide();
	lblAffichage->show();
	boxAffichage->setLayout(lytAffichage);

	layout3->addWidget(lblFPGA);
	boxFPGA->setLayout(layout3);

	layout -> addWidget(lblLumiere);
	layout -> addWidget(lblMusique);
	boxEtats -> setLayout(layout);
}

void Fenetre::setChronometre() {
	temps.start();
	chrono = new QLabel;
	chrono->setAlignment(Qt::AlignCenter);

	QFont newFont("Helvetica", 13, QFont::Bold, false);
	chrono ->setFont(newFont);
	chrono ->setText("0H 0M 0S");
	chrono ->setTextInteractionFlags(Qt::NoTextInteraction);

	connect(timer, SIGNAL(timeout()), this, SLOT(chronometre()));
}

void Fenetre::setLayoutPrincipal() {
	QHBoxLayout *lytHorizontal = new QHBoxLayout();
	QVBoxLayout *lytVertical = new QVBoxLayout();

    lytVertical->addWidget(boxBoutons);
    lytVertical->addWidget(txtZoneTexte);
	lytVertical->addWidget(boxFPGA);
	lytVertical->addWidget(chrono);
	lytHorizontal->addWidget(boxAffichage);
	lytHorizontal->addLayout(lytVertical);
	lytHorizontal->addWidget(boxEtats);
    setLayout(lytHorizontal);
}

int Fenetre::getTemperature() {
	return rand() % 100; // On determine une nouvelle temperature aleatoire
}